package com.servlet.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import DB.DBManager;

import com.servlet.vo.bean.Carrier;

public class CarrierDao {

	private Connection con;
	private Statement statement;
	private ResultSet resultSet;
	private boolean flag = false;
	private StringBuffer sql = new StringBuffer();
	private final String TABLE = "carrier";
	
	public CarrierDao() throws SQLException{
		con = DBManager.getConn();
		statement = con.createStatement();
	}
	public void close(){
		
		if(null != resultSet){
			try {
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(null != statement){
			try {
				statement.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(null != con){
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public boolean save(Carrier obj) {
		sql.append("INSERT INTO " + TABLE + "(ADDTIME,deleteStatus,acc_id,ca_name,availableBalance,updateTime) ");
		sql.append("VALUES( NOW(),FALSE,");
		sql.append(obj.getAcc_id() + ",'");
		sql.append(obj.getCa_name() + "',");
		sql.append(obj.getAvailableBalance() + ",NOW())");
		
		try {
			flag = statement.executeUpdate(sql.toString()) == 1;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			flag = true;
		}finally{
			close();
		}
		return flag;
	}
	
	public boolean delete(Long id){
		
		sql.append("UPDATE " + TABLE + " SET deleteStatus = TRUE WHERE id = '" + id + "'");
		try {
			flag = statement.executeUpdate(sql.toString()) == 1;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close();
		}
		return flag;
	}
	
	public boolean update(Carrier obj , boolean refreshAvailableBalance){
		
		try {
			sql.append("UPDATE " + TABLE + " set ");
			if(obj.getCa_name() != null && !obj.getCa_name().equals("")){
				sql.append("ca_name='" + obj.getCa_name() + "',");
			}
			if(refreshAvailableBalance){
				sql.append("availableBalance=" + obj.getAvailableBalance() + ",");
			}
			sql.append("updateTime = NOW() ");
			sql.append("where id = " + obj.getId());
			flag = statement.executeUpdate(sql.toString()) == 1;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close();
		}
		return flag;
	}
	
	public Carrier getObj(long id){
		Carrier obj = null;
		sql.append("SELECT id,addTime,deleteStatus,acc_id,ca_name,availableBalance,updateTime FROM " + TABLE );
		sql.append(" WHERE deleteStatus = FALSE and id = " + id);
		try {
			resultSet = statement.executeQuery(sql.toString());
			if(resultSet.next()){
				obj = new Carrier();
				obj.setId(resultSet.getLong("id"));
				obj.setAddTime(resultSet.getTimestamp("addTime"));
				obj.setDeleteStatus(resultSet.getBoolean("deleteStatus"));
				obj.setAcc_id(resultSet.getLong("acc_id"));
				obj.setCa_name(resultSet.getString("ca_name"));
				obj.setAvailableBalance(resultSet.getBigDecimal("availableBalance"));
				obj.setUpdateTime(resultSet.getTimestamp("updateTime"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return obj;
	}
	
	public List<Carrier> getList(){
		
		sql.append("SELECT id,addTime,deleteStatus,acc_id,ca_name,availableBalance,updateTime FROM " + TABLE + " WHERE deleteStatus = FALSE ");
		List<Carrier> objs = new ArrayList<Carrier>();
		try {
			resultSet = statement.executeQuery(sql.toString());
			Carrier obj = null;
			while(resultSet.next()){
			
				obj = new Carrier();
				obj.setId(resultSet.getLong("id"));
				obj.setAddTime(resultSet.getTimestamp("addTime"));
				obj.setDeleteStatus(resultSet.getBoolean("deleteStatus"));
				obj.setAcc_id(resultSet.getLong("acc_id"));
				obj.setCa_name(resultSet.getString("ca_name"));
				obj.setAvailableBalance(resultSet.getBigDecimal("availableBalance"));
				obj.setUpdateTime(resultSet.getTimestamp("updateTime"));
				objs.add(obj);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close();
		}
		return objs;
	}
	
	/**
	 * 通过 属性值 获取 消费载体集合
	 * @param params
	 * @return
	 */
	public List<Carrier> getListByProperties(Map<String, String> params){
		
		sql.append("SELECT id,addTime,deleteStatus,acc_id,ca_name,availableBalance,updateTime FROM " + TABLE + " WHERE deleteStatus = FALSE ");
		Iterator<String> iterator = params.keySet().iterator();
		while(iterator.hasNext()){
			String key = iterator.next();
			String value = params.get(key);
			sql.append(" and "+key+"="+value);
		}
		List<Carrier> objs = new ArrayList<Carrier>();
		try {
			resultSet = statement.executeQuery(sql.toString());
			Carrier obj = null;
			while(resultSet.next()){
			
				obj = new Carrier();
				obj.setId(resultSet.getLong("id"));
				obj.setAddTime(resultSet.getTimestamp("addTime"));
				obj.setDeleteStatus(resultSet.getBoolean("deleteStatus"));
				obj.setAcc_id(resultSet.getLong("acc_id"));
				obj.setCa_name(resultSet.getString("ca_name"));
				obj.setAvailableBalance(resultSet.getBigDecimal("availableBalance"));
				obj.setUpdateTime(resultSet.getTimestamp("updateTime"));
				objs.add(obj);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close();
		}
		return objs;
	}
}